package com.dushiguang.findhoney.View.Banner;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;

import com.dushiguang.findhoney.R;

import java.util.List;

/**
 * Created by ex-dushiguang201 on 2015-11-30.
 */
public class ViewPagerOfBanner extends ViewPager implements OnGestureListener {

    private GestureDetector mGestureDetector;
    private ImageHandle mImageHandle;
    private List<String> mList;
    private BannerHandler mBannerHandler;
    private int[] LocalPic;

    public ViewPagerOfBanner(Context context) {
        super(context);
        init();
    }

    public ViewPagerOfBanner(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mGestureDetector = new GestureDetector(getContext(), this);
    }

    /**********************ViewPager start****************/

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mGestureDetector.onTouchEvent(ev);
        if(mList != null && mList.size() <= 1) {
            return true;
        }
        return super.onTouchEvent(ev);
    }

    /**********************ViewPager END****************/

    /**********************implements OnGestureListener****************/
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        stopScroll();
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        Log.e("dushiguang","mImageHandle ====>" + mImageHandle);
        if (LocalPic.length != 0) {
            int position = getCurrentItem() % LocalPic.length;
            mImageHandle.onItemClick(String.valueOf(LocalPic[position]), position);
        }
        else if (mImageHandle != null && mList != null && mList.size() != 0) {
            int position = getCurrentItem() % mList.size();
            mImageHandle.onItemClick(mList.get(position), position);
        }
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }
    /**********************implements OnGestureListener END****************/

    public void setData(FragmentActivity activity, List<String> list, int []pic,ImageHandle handle) {
        setData(activity, list, pic, handle, R.layout.image_viewpager_item);
    }

    public void setData(FragmentActivity activity, List<String> list, int[]pic,ImageHandle handle, int itemResId) {
        this.mList = list;
        this.mImageHandle = handle;
        this.LocalPic = pic;
        setAdapter(new ViewPagerOfBannerAdapter(activity, list, LocalPic, handle, itemResId, this));
    }

    public void setFixedCurrentItem(int position) {
        setCurrentItem(getCurrentItem() / mList.size() * mList.size() + position, false);
    }

    public void setItemClick(ImageHandle handle) {
        this.mImageHandle = handle;
    }

    public BannerHandler getmBannerScrollHandler() {
        return mBannerHandler;
    }

    public void setmBannerScrollHandler(BannerHandler mBannerScrollHandler) {
        this.mBannerHandler = mBannerScrollHandler;
    }

    /**
     * 开始自动滚动
     */
    public void startScroll() {
        if (mBannerHandler != null && mList != null && mList.size() > 1) {// 1张图片不滚动
            mBannerHandler.start();
        }
    }

    /**
     * 停止滚动
     */
    public void stopScroll() {
        if (mBannerHandler != null) {
            mBannerHandler.stop();
        }
    }

    /**
     * 设置滚动间隔时间，单位:毫秒
     *
     * @param switchDelay
     */
    public void setScrollSwitchDelay(long switchDelay) {
        if (mBannerHandler != null) {
            mBannerHandler.setSwitchDelay(switchDelay);
        }
    }

}
